import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:riki_base_flutter/riki_base_flutter.dart';
import 'package:riki_login/res/header.dart';
import 'package:riki_login/riki_login.dart';
import 'package:riki_login/router/riki_login_router.dart';
import 'package:oktoast/oktoast.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  final RouteObserver<PageRoute> observer = RikiApplication.routeObserver;

  @override
  Widget build(BuildContext context) {
    RikiBase.init();
    return OKToast(
      position: ToastPosition.bottom,
      child: ScreenUtilInit(
        builder: () => MaterialApp(
          title: 'Flutter Demo',
          theme: ThemeData(
            primarySwatch: Colors.blue,
            visualDensity: VisualDensity.adaptivePlatformDensity,
          ),
          home: MyHomePage(title: 'Riki Login'),
        ),
      ),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key? key, required this.title}) : super(key: key);
  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  bool initStaff = false;
  bool initUser = false;

  String apiUrl = 'https://design-stage.meiwu365.com/';
  String h5Url = 'https://design-stage.meiwu365.com/static/';
  static const Color app_main = Color(0xFF35C4C4);

  void onPlatformAgreementClick(BuildContext context) {}

  void onPrivacyPolicyClick(BuildContext context) {}

  void onLoginSuccessCallback(BuildContext context) {}

  void onLoginDateCallback(BuildContext context) {}



  String cerName = 'assets/uatGomeHome.der';
  String version = '1.6.5';
  String wechatAppId = 'wx9a35e4bba636180c';
  String gmAppId = '57D03889892A1F6C100446522612ED69';
  bool isDebug = true;
  bool developLogin = false;


  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    ScreenUtil.init(
      BoxConstraints(maxWidth: MediaQuery.of(context).size.width, maxHeight: MediaQuery.of(context).size.height),
      designSize: Size(375, 667),
    );
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            _buildBtn('user初始化', () {
              if (initUser) {
                ToastUtil.show('已经初始化完成');
                return;
              }
              RikiLogin.instance.initUser(
                context: context,
                apiUrl: apiUrl,
                h5Url: h5Url,
                version: version,
                primaryColor: app_main,
                cerName: cerName,
                wechatAppId: wechatAppId,
                gmAppId: gmAppId,
                developLogin: false,
                isDebug: isDebug,
                onPlatformAgreementClick: onPlatformAgreementClick,
                onPrivacyPolicyClick: onPrivacyPolicyClick,
                onLoginSuccessCallback: onLoginSuccessCallback,
                onLoginDateCallback: onLoginDateCallback, wechatUniversalLink: 'https://dabanjia.com/',
              );
              initUser = true;
            }),
            _buildBtn('user登陆', () {
              if (!initUser) {
                ToastUtil.show('请点击初始化user');
                return;
              }
              NavigatorUtils.push(context, RikiLoginRouter.userLoginCode);
              // ToastUtil.show('初始化完成');
            }),
            _buildBtn('staff初始化', () {
              if (initStaff) {
                ToastUtil.show('已经初始化完成');
                return;
              }
              initStaff = true;
              RikiLogin.instance.initStaff(
                context: context,
                apiUrl: apiUrl,
                h5Url: h5Url,
                primaryColor: app_main,
                onPlatformAgreementClick: onPlatformAgreementClick,
                onPrivacyPolicyClick: onPrivacyPolicyClick,
                onLoginSuccessCallback: onLoginSuccessCallback,
              );
              ToastUtil.show('初始化完成');
            }),
            _buildBtn('staff登陆', () {
              if (!initStaff) {
                ToastUtil.show('请点击初始化staff');
                return;
              }
              NavigatorUtils.push(context, RikiLoginRouter.staffLoginCode);
              // ToastUtil.show('初始化完成');
            }),
          ],
        ),
      ),
    );
  }

  Widget _buildBtn(String title, VoidCallback onTap) {
    // ignore: deprecated_member_use
    return FlatButton(
      onPressed: onTap,
      color: Colors.blue,
      shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(100)),
      child: Text(
        title,
        style: TextStyle(color: Colors.white),
      ),
    );
  }
}
